<?php

namespace Common\Model;

/**
 * VipInfoModel.class.php实例模型
 *
 * @author zhangzhen
 * @date 2017-2-15
 */
class VipInfoModel extends BaseModel {

    protected $trueTableName = 'tb_vip_info';

    /**
     * 返回会员设置数据
     * @modify zhangzhen 2017-02-15    获取掌柜的会员设置信息
     * @param integer $hotelId 客栈ID
     * @return type
     */
    public function getInfoByHotel($hotelId) {
        $option = [
            'hotelentity_id'=> $hotelId,
        ];
        $field = 'id,hotelentity_id,name,auto,level,levelrule,discount,discountrule';
        $data = $this->field($field)->where($option)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 返回会员设置数据
     * @modify zhangzhen 2017-02-15    获取掌柜的会员设置信息
     * @param integer $hotelId 客栈ID
     * @return type
     */
    public function getInfoById($id) {
        $option = [
            'id'=> $id,
        ];
        $field = 'id,hotelentity_id,name,auto,level,levelrule,discount,discountrule';
        $data = $this->field($field)->where($option)->find();

        return empty($data) ? array() : $data;
    }
    /**
     * 返回会员设置数据
     * @modify zhangzhen 2017-02-15    获取掌柜的会员设置信息
     * @param integer $hotelId 客栈ID
     * @return type
     */
    public function getNextLevelById($hotelId,$levelRule) {
        $option = [
            'hotelentity_id'=> $hotelId,
            'levelrule' => array('gt',$levelRule),
        ];
        $field = 'id,hotelentity_id,name,auto,level,levelrule,discount,discountrule';
        $data = $this->field($field)->where($option)->order('levelrule asc')->find();
        return empty($data) ? array() : $data;
    }
    /**
     * 返回会员设置数据
     * @modify zhangzhen 2017-02-15    获取掌柜的会员设置信息
     * @param integer $hotelId 客栈ID
     * @param integer $name    等级名称
     * @return type
     */
    public function getInfoByHotelName($hotelId,$name) {
        $option = [
            'hotelentity_id'=> $hotelId,
            'name'=> $name,
        ];
        $field = 'id,hotelentity_id,name,auto,level,levelrule,discount,discountrule';
        $data = $this->field($field)->where($option)->find();

        return empty($data) ? array() : $data;
    }
    /**
     *
     * 添加用户数据
     * @modify zhangzhen 2017-02-15
     * @param integer $name           等级名称
     * @param integer $hotelId        客栈ID
     * @param string  $auto           自动升级
     * @param string  $level          升级
     * @param string  $levelRule      升级方式
     * @param string  $discount       打折
     * @param string  $discountRule   打折方式
     * @return type
     */
    public function addInfo($name, $hotelId, $auto, $level, $levelRule, $discount, $discountRule) {
        $data = [
            'name' => $name,
            'hotelentity_id' => $hotelId,
            'auto' => $auto,
            'level' => $level,
            'levelrule' => $levelRule,
            'discount' => $discount,
            'discountrule' => $discountRule,
        ];
        return $this->add($data);
    }

    /**
     * 更新用户数据
     * @modify zhangzhen 2017-02-16
     * @param integer $id             id
     * @param integer $name           等级名称
     * @param integer $hotelId        客栈ID
     * @param string  $auto           自动升级
     * @param string  $level          升级
     * @param string  $levelRule      升级方式
     * @param string  $discount       打折
     * @param string  $discountRule   打折方式
     * @return type
     */
    public function saveInfo($id, $name, $auto, $level, $levelRule, $discount, $discountRule) {
        $data = [
            'name' => $name,
            'auto' => $auto,
            'level' => $level,
            'levelrule' => $levelRule,
            'discount' => $discount,
            'discountrule' => $discountRule,
        ];
        $result = $this->where(array('id' => $id))->save($data);
        return $result === false ? false : true;
    }

}
